<?php

namespace app\api\controller;

use app\admin\model\service\Service;
use ba\Tree;
use Throwable;
use think\facade\Db;
use think\facade\Config;
use app\common\controller\Frontend;
use app\common\library\token\TokenExpirationException;

class Home extends Frontend
{
    protected array $noNeedLogin = ['*'];

    public function initialize(): void
    {
        parent::initialize();
    }

    /**
     * 前台和会员中心的初始化请求
     * @throws Throwable
     */
    public function index(): void
    {
        $classModel = new \app\admin\model\service\Classification;
        ;
        // 因为涉及到的项目类型不多 所以直接全部查询

        $info = $classModel
            ->with([
                'service' => function ($query) {
                    $query->whereIn('area_id', [0, $this->request->param('area_id')]);
                    $query->where('service_status', 1);
                    $query->where('type', 1);
                }
            ])
            ->select()->each(function ($item) {
                $item->service->each(function ($service) {
                    $service->service_cover_img = IMAGE_URL . $service->service_cover_img;
                    $service->service_introduce = IMAGE_URL . $service->service_introduce;
                    $service->service_taboo_statement = IMAGE_URL . $service->service_taboo_statement;
                    $service->service_ordering_notice = IMAGE_URL . $service->service_ordering_notice;
                });
            });

        $this->success("", $info);

    }

    /**
     * 获取加钟服务
     * @return void
     */
    public function clockService(): void
    {
        $info = Service::where('type', 2)->where('service_status', 1)->select()->each(function ($service) {
            $service->service_cover_img = IMAGE_URL . $service->service_cover_img;
            $service->service_introduce = IMAGE_URL . $service->service_introduce;
            $service->service_taboo_statement = IMAGE_URL . $service->service_taboo_statement;
            $service->service_ordering_notice = IMAGE_URL . $service->service_ordering_notice;
        });
        $this->success("", $info);
    }

    /**
     * 轮播图
     */
    public function banner()
    {
        $bannerModel = new \app\admin\model\service\Banner;
        $info = $bannerModel->select();
        if (!empty($info)) {
            $info->each(function ($item) {
                $item->image = IMAGE_URL . $item->image;
                return $item;
            });
        }
        $this->success("", $info);
    }


}